/**
产品
**/
function getParentProductClass(v,i){   
	$.get(APP+'/Ajax/getParentProductClass/id/'+i,function(data){
		data && $dom.find("#parent_info").html(lang['basic']['class_parent']+data)
	})
}        

function sumBag(obj){
	if(obj){
		var dom = $(obj).parents("form:first");
	}else{
		var dom = $dom;
	}
	if(dom.find("#dozen").length<=0) return;
	var c = parseInt(dom.find("#capability").val());
	var d = parseInt(dom.find("#dozen").val()); 
	if(isNaN(c)) c = 0;
	if(isNaN(d)) d = 0;
	if(c/d=='Infinity'){
		dom.find("#p_s_3").val('');
		return;
	}
	var bag = c/d;
	if(isNaN(bag) || bag==0) bag = ''; 
	dom.find("#p_s_3").val(bag); 
}

function sumQuantity(obj){
	
	var row = $(obj).attr('row');
	var cols = $(obj).attr('cols');
	var row_quantity = 0;
	var cols_quantity = 0;
	$dom.find("#fit").find("input[row='"+row+"']").each(function(){
		row_quantity += isNaN(parseInt(this.value)) ? 0 : parseInt(this.value);
	})
	$dom.find("#fit").find("#total_row"+row).html(row_quantity);
	
	$dom.find("#fit").find("input[cols='"+cols+"']").each(function(){
		cols_quantity += isNaN(parseInt(this.value)) ? 0 : parseInt(this.value);
	})
	$dom.find("#fit").find("#total_cols"+cols).html(cols_quantity);
	var all_quantity = 0;
	$dom.find("#fit").find("td[id^='total_row']").each(function(){
		all_quantity += isNaN(parseInt(this.innerHTML)) ? 0 : parseInt(this.innerHTML);
	});
	$dom.find("#fit").find("#all_total").html(all_quantity);
} 

function setBgColor(obj){
	var row = $(obj).attr('rowindex');
	var cols = $(obj).attr('colsindex');
	$dom.find("#fit").find("td").removeClass("selected_color");
	$dom.find("#fit").find("td[rowindex=1][colsindex="+cols+"]").addClass("selected_color");
	$dom.find("#fit").find("td[rowindex="+row+"][colsindex=1]").addClass("selected_color");
	$dom.find(obj).addClass("selected_color");
} 

function clearBgColor(){
	$("#fit").find("td").removeClass("selected_color");
}
/*角色使用 开始*/
function selectCheckbox(obj,id) {
	if(id){
		if($(obj).attr('checked')=='checked'){
			$dom.find("#"+id+" input:checkbox").attr('checked', true);
		}else{
			$dom.find("#"+id+" input:checkbox").attr('checked', false);
		}
	}
	//setParentCheckbox(obj);
};
function setParentCheckbox(obj){
	var parent_id = $dom.find(obj).attr('parent');
	if(parent_id){
		var i = 0;
		$dom.find("#"+parent_id+" input[type='checkbox']").each(function(){
			if($(this).attr("checked")){
				i++;
			}
		});
		var parent_obj = $dom.find("#"+parent_id+" input[type='checkbox']:first");
		if(i>0){
			if(i==1 && parent_obj.attr("checked")){
				parent_obj.attr("checked",false);
				if(parent_obj.attr('parent')){
					$.setParentCheckbox(parent_obj);
				}
				return;
			}
			parent_obj.attr("checked",true);
		}else{
			parent_obj.attr("checked",false);
		}
		if(parent_obj.attr('parent')){
			setParentCheckbox(parent_obj);
		}
	}
};
function setSelectValue(obj,id) {
	$dom.find("#"+id+" select").val(obj.value);
};
/*角色使用 结束*/

/* 用户 开始 */
function setCompanyUrl(v){
	$dom.find("#company_id").val('');
	$dom.find("#company").val('');
	if(v>1){
		$dom.find("#company_tr").show();
		if(v==2){
			$dom.find("#company").attr("url",$dom.find("#url_2").val()).initAutocomplete();
		}else{
			$dom.find("#company").attr("url",$dom.find("#url_3").val()).initAutocomplete();
		}
	}else{
		$dom.find("#company_tr").hide();
	}
}
/* 用户 结束 */

/* 国家 开始 */
function getCity(obj,dom){
	var country_id	= $(obj).val();
	if(dom){
		var self_dom = $("#"+dom);
	}else{
		var self_dom = $dom;
	}
	$.get(APP+'/Ajax/getCity',{id:country_id},function(data){
		self_dom.find("#city").html(data);
		self_dom.find("input[name='city_name']").initAutocomplete();
	});
};

/* 国家 结束 */


/* 订单 开始 */
function getOrderFacCur(obj){
	if(!obj.value) {
		$(obj).parents("tr:first").find("#currency_id").val('');
		$(obj).parents("tr:first").find("#span_fac_name").html('');
		return;
	}
	var url = APP+'/Ajax/getOrderFacCur/id/'+obj.value;
	$.getJSON(url,function(data){
		$(obj).parents("tr:first").find("#currency_id").val(data.currency_id);
		$(obj).parents("tr:first").find("#span_fac_name").html(data.factory_name);
		$(obj).parents("tr:first").find("#factory_id").val(data.factory_id);
	});
	getDataByOrderId(obj);
	
};
/* 订单 结束 */
/* 退换货*/
function getReturnSaleType(obj){
if(obj.value==2){
		$dom.find("#show_sale_order_tr").show();
		$dom.find("#show_sale_order").show();
		$dom.find("#tr_barcode").show();		
	}else{
		$dom.find("#show_sale_order_tr").hide();
		$dom.find("#show_sale_order").hide();  
		$dom.find("#tr_barcode").hide();
	}
}
// 产品绑定厂家时获取产品输入框
function getProductNo(){	 
	var client_id	=	$dom.find("#client_id").val();
	var basic_id	=	$dom.find("#basic_id").val();
//	var currency_id	=	$("#currency_id").val(); 
	if(client_id>0 && basic_id>0){
		$.post(APP+"/Ajax/getReturnSaleProductNoInput",{client_id:client_id,basic_id:basic_id},function(data){   
	        if (data != false) {        	
	        	$dom.find("#show_return_order input[name*='product_no]']").each(function(){ 
	        		$(this).attr({'where':encodeURIComponent(data),'jqac':true});
	        		$(this).initAutocomplete();
	        	});	 
	        };
		});
	} 
}

/* 退换货结束*/
/* 通过厂家获取改厂家的默认币种 */
function getFacCurrencyId(obj,type){
	var fac_id	= $(obj).val();
	var type	= type ? type : 1;
	if(fac_id>0){
		$.getJSON(APP+'/Ajax/getFacCurrencyId',{id:fac_id},function(data){
			if(type==1){
				$dom.find('#currency_id').val(data.id);
				$dom.find('#currency_id').next('span').find('input').val(data.no);
			}else{
				var parent	= $(obj).parent('td').parent('tr');
				$(parent).find('#currency_id').val(data.id);
				$(parent).find('#currency_id').val(data.id);
				$(parent).find('#currency_id').next('span').find('input').val(data.no);
				if(data.product_factory>0){
					$(parent).find('input[name*=product_id]').val('');
					$(parent).find('#span_product_name').html('');
					$(parent).find("input[name*='product_no]']").removeAttr("disabled").removeClass("disabled").val('').attr({'where':encodeURIComponent('factory_id='+fac_id),'jqac':true});
					$(parent).find("input[name*='product_no]']").initAutocomplete();
				}
			}
		});
	}
}
/*同过产品获取产品名称和单价*/
function getProductInfo(obj){
	var p_id 	= $(obj).val();
	if(p_id>0){
		$.getJSON(APP+'/Ajax/getProductInfoById',{id:p_id},function(data){
			$(obj).parents("tr:first").find("#span_product_name").html(data.product_name);
			$(obj).parents("tr:first").find("#price").val(data.dml_instock_price);
		});
	}
}

/* 装柜时根据订单获取订单的产品 */
function getOrderProduct(obj){
	var obj_parent  = $(obj).parent('td').parent('tr');
	var order_id 	= $(obj_parent).find('#order_id').val();
	var product_id 	= $(obj_parent).find('#product_id').val();
	var where;
	if(order_id>0){
		where = encodeURIComponent("orders_id="+order_id);
		$(obj).parents("tr:first").find("input[name*='product_no]']").attr({'where':where,"jqac":true});
		$(obj).parents("tr:first").find("input[name*='product_no]']").initAutocomplete();
	}else if(product_id>0){
		where = encodeURIComponent("product_id="+product_id);
		$(obj).parents("tr:first").find("input[name=order_no]").attr({'where':where,"jqac":true,'url':APP+'/AutoComplete/orderNoByProduct'});
		$(obj).parents("tr:first").find("input[name=order_no]").initAutocomplete();
	}
}
// 装柜明细中根据单号来查找装柜信息
//function getDataByOrderId(obj) {
//	getOrderProduct(obj);
//	var obj_parent  = $(obj).parent('td').parent('tr');
//	var p_id		= $(obj_parent).find('#product_id').val();		
//	var order_id	= $(obj_parent).find('#order_id').val();
//	var color_id	= $(obj_parent).find('#color').val();
//	var size_id		= $(obj_parent).find('#size').val();
//	if(order_id>0 || p_id >0){
//		$.getJSON(APP+'/Ajax/getOrderDetails', {order_id:order_id,p_id:p_id,color_id:color_id,size_id:size_id}, function(data){
//			if (data[0] != null) {
//				var show_capbility	= data.show_capbility
//				data = data[0];			
//				p_id>0 && $(obj_parent).find("#span_product_name").html(data.product_name);	
//				var where_au	= '';
//				if(p_id>0){
//					where_au	= " product_id="+p_id;
//					order_id>0 && " and order_id="+order_id;
//					//颜色
//					var color_where = encodeURIComponent(where_au);					
////					$(obj_parent).find("#color").val(data.color_id);
//					$(obj_parent).find("input[name=color_name]").attr({'where':color_where,'jqac':true,'url':$.U('/AutoComplete/orderProductColor')})/*.val(data.color_name)*/;
//					$(obj_parent).find("input[name=color_name]").initAutocomplete();
//					//尺码
////					$(obj_parent).find("#size").val(data.size_id+where_au);
//					$(obj_parent).find("input[name=size_name]").attr({'where':color_where,'jqac':true,'url':$.U('/AutoComplete/orderProductSize')})/*.val(data.size_name)*/;
//					$(obj_parent).find("input[name=size_name]").initAutocomplete();	
//				}
//				//单价
//				$(obj_parent).find('#price').val(data.edml_price);
//				//规格
//				if(show_capbility>0){
//					$(obj_parent).find('#capability').val(data.order_capability);
//					$(obj_parent).find('#dozen').val(data.order_dozen);
//					$(obj_parent).find('#pieces').val(data.order_pieces);
//				}
//				$(obj_parent).find('#order_qn').html(data.edml_order_qn);
//				$(obj_parent).find('#unload_qn').html(data.edml_unload_quantity);
//			}else{
//				$(obj).val('');
//				$(obj).next().val('');
//			}
//		});
//	
//	}
//}
// 装柜明细中根据产品号来获取颜色/尺码信息
	function getDataByIds(obj) {		
		var obj_parent  = $(obj).parent('td').parent('tr');
		var order_id	= $(obj_parent).find('#order_id').val();
		var p_id		= $(obj_parent).find('#product_id').val();
		var obj_id		= $(obj).attr('id');
		var id			= $(obj).val();
		var color_id	= 0;	
		var size_id		= 0;	
		var param		= '';
		var index		= $(obj_parent).attr('index');						
		if ((order_id > 0 || p_id > 0) && id > 0) {			
			if (obj_id == 'order_id') {	
					if(p_id > 0) { param = {'order_id':id,'p_id':p_id}} else {	param = {'order_id':id}};				
			} else if (obj_id == 'product_id') { // 产品号
				$(obj_parent).find('#autoshow_img').attr('pid',id);				
				if($(obj_parent).find('#order_no').val() != '' && order_id > 0) {
					param = {'order_id':order_id, 'p_id':id};
				} else {
					param = {'p_id':id};
				}				
			}else if (obj_id == 'color') {  // 颜色						
				param = {'order_id':order_id, 'p_id':p_id, 'color_id':id};							
			}else if (obj_id == 'size') { // 尺码
				if ($(obj_parent).find("#color").length > 0) {
					param = {'order_id':order_id, 'p_id':p_id, 'color_id':$(obj_parent).find("#color").val(), 'size_id':id};
				} else {
					param = {'order_id':order_id, 'p_id':p_id, 'size_id':id};
				}				
			}		
			$.getJSON(APP+"/Ajax/getOrderDetails", param, function(data){				
				if (data[0] != null) {		
					var per_size 		= data.per_size;
					var weight   		= data.weight;
					var show_capbility	= data.show_capbility;
					var show_delivery	= data.show_delivery;
					var show_color		= data.show_color;
					var show_size		= data.show_size;
					data 				= data[0];
					p_id>0				&& $(obj_parent).find("#span_product_name").html(data.product_name);
					if(p_id>0 && order_id>0){
						where_au	= " product_id="+p_id+" and orders_id="+order_id;
					}else if(p_id>0){
						 where_au	= " product_id="+p_id;
					}else if(order_id>0){
						 where_au	= " orders_id="+order_id;
					}
					if(order_id>0){
						where = encodeURIComponent("orders_id="+order_id);
						$(obj_parent).find("input[name*='product_no]']").attr({'where':where,"jqac":true});
						$(obj_parent).find("input[name*='product_no]']").initAutocomplete();
					}
					$(obj_parent).find('#span_fac_name').html(data.factory_name);
					$(obj_parent).find("#currency_id").val(data.currency_id);
					$(obj_parent).find("#factory_id").val(data.factory_id);
					if(p_id>0){
						where = encodeURIComponent("product_id="+p_id+' and order_details.detail_state<3');
						$(obj_parent).find("input[name=order_no]").attr({'where':where,"jqac":true,'url':APP+'/AutoComplete/orderNoByProduct'});
						$(obj_parent).find("input[name=order_no]").initAutocomplete();
					}
					if(order_id>0 || p_id>0){
						if(show_color==2  && p_id>0 ){
							var color_where = encodeURIComponent("product_id="+p_id);
							$(obj_parent).find("#color").val(data.color_id);
							$(obj_parent).find("input[name=color_name]").attr({'where':color_where,'jqac':true,'url':APP+'/AutoComplete/productColor'}).val(data.color_name);
							$(obj_parent).find("input[name=color_name]").initAutocomplete();
						}else if(show_color==1 && (order_id>0 || p_id>0)){
							//颜色
							var color_where = encodeURIComponent(where_au);					
							$(obj_parent).find("#color").val(data.color_id);
							$(obj_parent).find("input[name=color_name]").attr({'where':color_where,'jqac':true,'url':APP+'/AutoComplete/orderProductColor'}).val(data.color_name);
							$(obj_parent).find("input[name=color_name]").initAutocomplete();
						}else{
							$(obj_parent).find("input[name=color_name]").attr({'jqac':true,'url':APP+'/AutoComplete/color'}).val(data.color_name);
							$(obj_parent).find("input[name=color_name]").initAutocomplete();
						}
						if(show_size==2 && p_id>0){
							var color_where = encodeURIComponent("product_id="+p_id);
							$(obj_parent).find("#size").val(data.size_id);
							$(obj_parent).find("input[name=size_name]").attr({'where':color_where,'jqac':true,'url':APP+'/AutoComplete/productSize'}).val(data.size_name);
							$(obj_parent).find("input[name=size_name]").initAutocomplete();	
						}else if(show_size==1  && (order_id>0 || p_id>0)){
							//尺码
							var color_where = encodeURIComponent(where_au);	
							$(obj_parent).find("#size").val(data.size_id);
							$(obj_parent).find("input[name=size_name]").attr({'where':color_where,'jqac':true,'url':'/AutoComplete/orderProductSize'}).val(data.size_name);
							$(obj_parent).find("input[name=size_name]").initAutocomplete();	
						}else{
							$(obj_parent).find("input[name=size_name]").attr({'jqac':true,'url':APP+'/AutoComplete/size'}).val(data.size_name);
							$(obj_parent).find("input[name=size_name]").initAutocomplete();	
						}
					}
					//单价
					$(obj_parent).find('#price').val(data.edml_price);
					//规格
					if(show_capbility>0){
						$(obj_parent).find('input[name*=capability]').val(data.order_capability);
						$(obj_parent).find('input[name*=dozen]').val(data.order_dozen);
					}
					$(obj_parent).find('#order_qn').html(data.edml_order_qn);
					$(obj_parent).find('#unload_qn').html(data.edml_unload_quantity);
					if(show_delivery>0){
						$(obj_parent).find('input[name*="per_size]"]').val(per_size);
						$(obj_parent).find('input[name*="per_capability]"]').val(weight);
					}
					$(obj_parent).find('input[name*="quantity]"]').trigger('keyup');
				} 
			}); 
		}
	}


	// 获取未装柜的订单(用于新装柜)
	function getUnLoadOrders(flow_color,flow_size,orders_norms) {
		var order_id 	= $dom.find('#order_id').val();
		var fac_id	 	= $dom.find('#factory_id').val();
		var p_id		= $dom.find('#product_id').val();
		if(order_id>0 || fac_id >0 || p_id >0){
			$dom.find('#loadContrain').load(APP+'/Orders/getUnloadOrder',{order_id:order_id,fac_id:fac_id,p_id:p_id});
		}else{
			alert('请输入要查询的信息!');
		}
	};
	
	// 清空通过查询到找的待装柜信息
	$.clearData	= function() {
		$dom.find('#loadContrain').html('');
	}

	// 装柜/整单装柜
	function load(detail_id, order_id,obj) { 
		if (detail_id > 0 || order_id > 0) {
			$.getJSON(APP+"/Ajax/getLoadDetails", {'detail_id':detail_id, 'order_id':order_id}, function(data){
				if (data.list != null) {		
					$.each(data.list, function(k,item){
						var first_obj 	= $dom.find('#detail_table:last').find('tbody').find('tr:visible:first');
						var list_row	= $dom.find('#detail_table:last').find('tbody').find('tr:visible').size();
						for (i = 1; i <= list_row; i++)
						{ 
							if (first_obj.find("#order_id").val() < 1 || (!first_obj.find("#order_no").length>0 && first_obj.find("#order_no").val())){
								first_obj	=	first_obj;
							}else{
								if(i==list_row){
									first_obj	=	first_obj;
								}else{
									first_obj	=	first_obj.next();
								} 
							}	 	
						}
						if (first_obj.find("#order_id").val() < 1 || (!first_obj.find("#order_no").length>0 && first_obj.find("#order_no").val())){	
							var new_obj = first_obj;	
						} else {			
							var new_obj = $.copyRowWithFrom($(first_obj).find('td:last').find('span[class*="icon-add-plus"]'));	
						}								
						$(new_obj).find("#order_id").val(item.id);
						$(new_obj).find("#autoshow_img").attr('pid',item.product_id);
						$(new_obj).find("#factory_id").val(item.factory_id);
						$(new_obj).find("#currency_id").val(item.currency_id);
						$(new_obj).find("#order_no").val(item.order_no);
						$(new_obj).find("#span_fac_name").html(item.factory_name);						
						$(new_obj).find("#product_id").val(item.product_id);
						$(new_obj).find("input[name*='product_no]']").val(item.product_no);
						$(new_obj).find("#span_product_name").html(item.product_name);	
						$(new_obj).find("#color").val(item.color_id);					
						$(new_obj).find("input[name=color_name]").val(item.color_name);					
						$(new_obj).find("input[name*=size]").val(item.size_id);						
						$(new_obj).find("input[name=size_name]").val(item.size_name);
						$(new_obj).find("input[name*=price]").val(item.edml_price);
						if(data.storage_format>0){
							$(new_obj).find('input[name*=capability]').val(item.order_capability);
							$(new_obj).find('input[name*=dozen]').val(item.order_dozen);	
						}
						$(new_obj).find('#currency_id').val(item.currency_id);
						$(new_obj).find('#order_qn').html(item.sum_capability);	//订货数量 
						$(new_obj).find('#unload_qn').html(item.min_capability);	//订货数量 
						if(data.show_delivery>0){
							$(new_obj).find('input[name*="per_size]"]').val(item.edml_per_size);
							$(new_obj).find('input[name*="per_capability]"]').val(item.dml_weight);
						}	
					});
					if(detail_id>0){
						$(obj).parents("tr:first").remove();
					}else{
						$dom.find("div[id=loadContrain]").find("tr[order_id='"+order_id+"']").remove();
					}
				}else{
					alert('失败了,请联系系统管理员!!')
				};
			});
		};
	}
	//通过销售单选择的币种显示银行信息
	function getBankCurrencry(currency_id){ 
		$dom.find('#currency_id').val(currency_id);
		//如果已存在银行插入的币种与选的币种提示客户回退 
		if(currency_id>0){
			$dom.find('#pay_transfer_bank_id').val('');
        	$dom.find('input[name=pay_transfer_bank_name]').val('').attr({'where':encodeURIComponent('currency_id='+currency_id),'jqac':true});
        	$dom.find('input[name=pay_transfer_bank_name]').initAutocomplete();   	 				  
		} 
	 	
	}
	/****************销售预付款jS**********************************/
		var s	=new Array('','cash','bill','transfer');　  
		function addPaidType(v){     
			for(m=1;m<4;m++){
				$dom.find("#"+s[m]).hide();
			} 
			$dom.find("#"+s[v]).show();
			var old_paid_type	=	$dom.find("#pay_paid_type").val();  
			if(old_paid_type>0){ 
				clearPaidType(old_paid_type)
			}   
			$dom.find("#pay_paid_type").val(v);  
			$.dealWithPM();//计算销售单金额
			var preferential_money 				= $.turnToPoint($dom.find('#pr_money').val());//优惠金额		
			var sum_after_dis_money				= $dom.find('td[total_col_dis_money]').html()!=null ? $.turnToPointShow($dom.find('td[total_col_dis_money]').html()) :$.turnToPointShow($dom.find('td[total_col_money]').html());//优惠后金额	  
			$.realMoneyWithSale(sum_after_dis_money,preferential_money);
			$dom.find("#"+s[v]).find("#user_type").val(v);   // 设置Select的Value值为4的项选中  
//			if(s[v]=='transfer'){
//				var	currency_id		= $dom.find('#currency_id').val();
//				var	flow			= $dom.find('#flow').val();
//				if(flow=='sale'){
//					$dom.find('input[name=pay_transfer_bank_name]').attr({'where':encodeURIComponent('currency_id='+currency_id),'jqac':true});
//					$dom.find('input[name=pay_transfer_bank_name]').initAutocomplete();
//				}
//			}
		}
		//清除默认信息
		function clearHtmlValueArray(clear_array){ 
			for(var i in clear_array){  
				$dom.find("#"+clear_array[i]).val('');  
			}	
		}
		
	/**************************************************/
//发票获取IVA
function getIVA(obj,from){
	var id = $(obj).val();
	if (id > 0) {
		$.getJSON(APP+"/Ajax/getIva",{id:id,from:from},function(data){
			$dom.find('#iva').val(data.iva);
			var import_type	= $dom.find('#import_type').val();
			switch(import_type){
				case 'instock':
					if(data.factory_from==1&&data.product_factory==1){
						$dom.find('input[name=relation_no]').attr('where',encodeURIComponent("factory_id="+id));
						var where	= encodeURIComponent("factory_id="+id);
						$dom.find('.detail_list tr').find('input[name*=product_no]').attr('where',where);
						$dom.find('.detail_list tr').find('input[name*=product_name]').attr('where',where);
						$dom.find('.detail_list tr').each(function(){
							$(this).find('input[jqac]').initAutocomplete();
						});
					}else{
						$dom.find('input[name=relation_no]').removeAttr('where');
					}
					$.sumTax();
					return ;
					break;
				case 'saleorder':
					if(data.invoice_from==1){
						if(data.connect_client>0){
							$dom.find('input[name=relation_no]').attr('where',encodeURIComponent('client_id='+data.connect_client));
						}else{
							$dom.find('input[name=relation_no]').removeAttr('where');
						}
					}else{
						$dom.find('input[name=relation_no]').attr('where',encodeURIComponent('client_id='+id));
					}
					break;
				case 'return':
					if(data.invoice_from==1){
						if(data.connect_client>0){
							$dom.find('input[name=relation_no]').attr('where',encodeURIComponent('client_id='+data.connect_client));
						}else{
							$dom.find('input[name=relation_no]').removeAttr('where');
						}
					}else{
						$dom.find('input[name=relation_no]').attr('where',encodeURIComponent('client_id='+id));
					}
					break;
				default:
					$dom.find('input[name=relation_no]').removeAttr('where');
					break;
			}
			$dom.find('input[name=relation_id]').val('');
			$dom.find('input[name=relation_no]').val('').initAutocomplete();
		});
	}
}
//显示支票号
function displayCheckNo() {
	var pay_type = $dom.find('select[name=paid_type] option:selected').val();
	if (pay_type == 2) {
		$dom.find('#check_td_1').show();
		$dom.find('#check_td_2').show();
	}else {
		$dom.find('#check_td_1').hide();
		$dom.find('#check_td_2').hide();
	}
}
//发票导入
function importData(){
	var relation_id	= $dom.find('#relation_id').val();
	var import_type	= $dom.find('#import_type').val();
	var factory_id	= $dom.find('#factory_id').val();
	if(import_type=='instock'&&factory_id>0){
		$dom.find('.detail_list').find('tr').each(function(){
			$(this).find('#span_product_name,#total_money,#span_invoice_ingredient').html('');
			$(this).find('input').val('');
			$(this).find('td[total_quantity],td[total_col_money],td[tax_total_cost],td[tax_cost]').html('');
		});
	}
	if(relation_id>0){
		$dom.find('.detail_list').find('input').val('');
		$dom.find('.detail_list').find('tr').each(function(){
			$(this).find('#span_product_name').html('');
			$(this).find('#total_money').html('');
			$(this).find('#span_invoice_ingredient').html('');
		});
		$.ajax({
			type: "POST",
			url: APP+"/Ajax/getRelationInfo",
			dataType: "json",
			data:{relation_id:relation_id,import_type:import_type,factory_id:factory_id},
			cache: false,
			async: false,
			success: function(result){
				if(result.detail){
					var first_index	= $dom.find('.detail_list tbody tr').not('.none').first().attr('index');
					var index		= first_index;
					var obj			= $dom.find('.detail_list tbody tr[index='+index+']');
					$.each(result.detail,function(k,item){
						if($(obj).attr('index')==undefined){
							$.copyRowWithFrom($dom.find('.detail_list tbody').find("tr:last").find('td'));
							obj		= $dom.find('.detail_list tbody').find("tr:last");
						}
//						if(index>len){
//							$.copyRowWithFrom($dom.find('.detail_list tbody').find("tr:last").find('td'));
//						}
//						var obj	= $dom.find('.detail_list tbody').find("tr[index="+index+"]");
						$(obj).find("input[name*='product_no]']").initAutocomplete();
						$(obj).find("input[name*='product_id']").val(item.product_id);
						$(obj).find("input[name*=product_no]").val(item.product_no);
						$(obj).find("input[name*='product_name']").val(item.product_name);
						$(obj).find('#span_product_name').html(item.product_name);
						$(obj).find("input[name*='price']").val(item.edml_price);
						$(obj).find("input[name*='quantity']").val(item.edml_sum_quantity);
						$(obj).find("#total_money").html(item.edml_money);
						$(obj).find('input[name*=discount]').val(item.edml_discount);
						$(obj).find('td[total_row_dis_money]').html(item.dml_discount_money);
						$(obj).find('#span_invoice_ingredient').html(item.ingredient);
						$.sumTotal($(obj).find('input[name*=product_id]'),'');
						obj	= $(obj).next();
					});
					$.sumTax();
				}
			}
		});
	}
}
//发票类型
function setInvoiceType(){
	var invoice_type	= $dom.find('input[name=invoice_type]:checked').val();
	if(invoice_type==2){
		$dom.find('#lang_order_no').html(lang['orders']['return_no']+'：');
		$dom.find('input[name=relation_id]').val('');
		$dom.find('#import_type').val('return');
		$dom.find('input[name=relation_no]').attr('url',APP+'AutoComplete/invoiceReturnOrderNo').val('').initAutocomplete();
	}else{
		$dom.find('#lang_order_no').html(lang['orders']['sale_no']+'：');
		$dom.find('input[name=relation_id]').val('');
		$dom.find('#import_type').val('saleorder');
		$dom.find('input[name=relation_no]').attr('url',APP+'AutoComplete/invoiceSaleOrderNo').val('').initAutocomplete();
	}
}
//销售付款转账,获取银行名称,暂时
function getBankName(obj){
	var id	= $(obj).val();
	if(id>0){
		$.post(APP+"/Ajax/getBankName",{id:id},function(data){
			$dom.find('#span_bank_name').val(data);
		});
	}
}
// 产品类别中地区管理表单验证特别处理
function productClassDistrict(result){
	var fid		= $dom.find("#form_name").val(); 
	var element = $dom.find('#'+fid); 
	if(result.status==1){
		$('#loader').css("display","none");
		$("#tips").html(result.info).fadeIn().delay(1000).fadeOut(400);
		loadTab();
	}else if(result.status==2){
		validity(result.info,element);
	}
} 
function setFormName(v){
	$dom.find("#form_name").val(v);
}
/**************转回国内开始*******************/
//转回国内银行
function getBankById(obj){
	var bank_id	= $(obj).val();
	if(bank_id>0){
		$.getJSON(APP+"/Ajax/getBankCurrencyId", {'bank_id':bank_id}, function(data){
			if(data){
				$dom.find('#in_bank_id').val('');
				$dom.find('input[name*="in_account_name"]').val('').attr({'where':encodeURIComponent('currency_id='+data.currency_id+' and id!='+bank_id),'jqac':true});
				$dom.find('input[name*="in_account_name"]').initAutocomplete();
			}
		});
	}
}

//根据币种限制银行
function getBankByCurrency(obj){
	var currency_id	 = $(obj).val();
	if(currency_id>0){
		//转出银行
		$dom.find('input[name*="account_name"]').val('').attr({'where':encodeURIComponent('currency_id='+currency_id),'jqac':true});
		$dom.find('input[name*="account_name"]').initAutocomplete();
		//转入银行
		$dom.find('input[name*="in_account_name"]').val('').attr({'where':encodeURIComponent('currency_id='+currency_id),'jqac':true});
		$dom.find('input[name*="in_account_name"]').initAutocomplete();
	}
}

//转回国内计算
function getTotal(obj){
	var quantity	= $.cParseInt($dom.find('input[name*="quantity"]').val());//笔数
	var money		= $.cParseFloat($dom.find('input[name*="money"]').val());//金额
	var other_cost	= $.cParseFloat($dom.find('input[name*="other_cost"]').val());//每笔费用
	$dom.find('#sum_money').html($.turnToComma(eval(quantity+'*'+money),2));
	$dom.find('#sum_other_cost').html($.turnToComma(eval(quantity+'*'+other_cost),2));
	
	
}
/**************转回国内结束*******************/
//销售保存的时候,如果为已配货,提示用户
function checkSaleaState(formData, jqForm, options)  {
	if ($dom.find('#pre_delivery').val() ==2) {
		if (!confirm('该销售单已配货,是否重新配货?')) {
			$.removeLoading();
			return false;
		}
	}
	return true;
}

//入库检验 汇率
function checkInstockRate(formData, jqForm, options){
	var rn =true;
	$.ajax({
		url:APP+'/Ajax/validRate',
		type:'post',
		data:formData,
		async:false,
		dataType: "json",
		success:function(data){
			if(data){
				if(confirm(data['info'])){
					rn =true;
				}else{
					rn	= false;
					$('#s_loading').hide();
					return false;
				}
			}
		}
	});
	return rn;
}

/*多个产品库存显示*/
function multiStorage(pid,obj){
	if(pid<=0) return;
	if($dom.find("#product_"+pid).length>0){
		$("<div>"+lang['stat']['product_is_last_page']+"</div>").dialog({
			//		modal: true,
			buttons: {
				Ok: function() {
					$( this ).dialog( "close" );
				}
			}
		});
	}else{
		var url = $(obj).attr("url");
		$.ajax({
			type: "POST",
			url: url,
			data: "id="+pid,
			dataType: "text",
			cache: false,
			async: false,
			success: function(html){
				$dom.find("#print").append(html);
			}
		});
	}	
}
/*销售根据客户和公司获取欠款*/
function getClientInfo(){
	var client_id	= $dom.find('#client_id').val();
	var basic_id	= $dom.find('#basic_id').val();
	$.getJSON(APP+'/Ajax/getClientInfo',{id:client_id,basic_id:basic_id},function(data){
		$dom.find('#client_fund_show').html(data.client_money);
		$dom.find('#receive_addr').val(data.edit_address);
	});
}


// 产品类别中地区管理表单验证特别处理
function statCheck(result){
	var element = $dom.find('#form'); 
	if(result.indexOf('status')==2){
		var result	= eval('(' + result + ')');
	}
	if(result.status==2){
		$dom.find('#print').hide();
		validity(result.info,element);
	}else{
		$dom.find('#print').show();
	}
} 
//新增产品和修改产品计算每箱立方
function getToTtalCube(obj){
	var parent		= $(obj).parent('td');
	var cube_long	= $.cParseFloat($(parent).find('#cube_long').val());
	var cube_wide	= $.cParseFloat($(parent).find('#cube_wide').val());
	var cube_high	= $.cParseFloat($(parent).find('#cube_high').val());
	$(parent).find('#cube').val($.cParseFloat(cube_long*cube_wide*cube_high).toFixed(cube_length));
}


function checkStatProduct(){
	var pid = $dom.find("#stat_p_id").val();
	if(pid){
		return true;
	}else{
		validity({"name":"product_id","value":lang['common']['require']},$dom,'.search_box');
		return false;
	}
	return false;
}

//导出公共js类
function ExportMytable(type){
	var queryString = $dom.find('#search_form').formSerialize();
	$dom.find('#ac_search').trigger('click');
	var rand		= $dom.find('#rand').val();
	var src 		= APP+"/Ajax/outPutExcel/state/"+type+'/rand/'+rand;
	var iframe		= $("<iframe style='display:none' name='iframe' src="+src+"></iframe>");
	$dom.find('iframe[name="iframe"]').remove();
	$dom.find('#search_form').append(iframe);
}


function quicklyAddCallback(module,result){
	switch (module){
		case 'Factory':
			$dom.find("#factory_id").val(result.id).next().val(result.comp_name);
		break;
		case 'Color':
			$dom.find("#add_color").before('<input type="checkbox" name="color[]" value="'+result.id+'" onclick="" checked>'+result.color_name+"&nbsp;&nbsp;");
		break;
		case 'Size':
			$dom.find("#add_size").before('<input type="checkbox" name="size[]" value="'+result.id+'" onclick="" checked>'+result.size_name+"&nbsp;&nbsp;");
		break;
		case 'Department':
			$dom.find("#department_id").val(result.id).next().val(result.department_name);
		break;
		case 'Position':
			$dom.find("#position_id").val(result.id).next().val(result.position_name);
		break;
		case 'Employee':
			$dom.find("#employee_id").val(result.id);
			$dom.find("#employee_name").val(result.employee_name);
		break;
		case 'PayClass':
			$dom.find("#pay_class_id").val(result.id).next().val(result.pay_class_name);
		break;
		case 'Product':
			ordersProduct(result);
		break;
		case 'Client':
			$dom.find("#client_id").val(result.id).trigger("change").next().val(result.comp_name);
		break;
		
	}
	
	function ordersProduct(data){
		if(data.product_factory==1){// 判断产品是否绑定厂家
			var factory_id = $dom.find("#factory_id").val();
			if(factory_id>0){// 是否已选择厂家未选择时退出
				if(data.factory_id!=factory_id){//判断是否选择的厂家与添加产品的厂家一致
					return false;
				}
			}else{
				return false;
			}
		}
		var first_obj 	= $dom.find('.detail_list:last').find('tbody').find('tr:visible:first');
		var list_row	= $dom.find('.detail_list:last').find('tbody').find('tr:visible').size();
		for (i = 1; i <= list_row; i++)
		{ 
			if (first_obj.find("input[name*='product_id]']").val() < 1 || !first_obj.find("input[name*='product_no]']").val()){
				first_obj	=	first_obj;
			}else{
				if(i==list_row){
					first_obj	=	first_obj;
				}else{
					first_obj	=	first_obj.next();
				} 
			}	 	
		}
		if (first_obj.find("input[name*='product_id]']").val() < 1 || !first_obj.find("input[name*='product_no]']").val()){	
			var obj = first_obj;	
		} else {			
			var obj = $.copyRowWithFrom($(first_obj).find('td:last').find('span[class*="icon-add-plus"]'));	
		} 
		$(obj).find('input[name*="product_no]"]').val(data.product_no);
		$(obj).find('input[name*="product_id]"]').val(data.id).trigger('change');
		return false;
	}
}
//装柜的时候如果装柜数量大于订货数量,给于错误提示
//修改订货的时候,不允许小于装柜数量
function checkLoadQuantity(obj,load_qn,flow){
	var parent		= $(obj).parent('td').parent('tr');
	$dom.find("#form_error_msg").css('display','none');
	if(flow=='orders'){
		var order_qn	= $.turnToPointShow($(parent).find('td[id=diff_quantity]').html());
		var re			= load_qn<order_qn;
		var err			= lang['orders']['error_order_quantity'];
	}else{
		var order_qn	= $.turnToPointShow($(parent).find('td[id=unload_qn]').html());
		var re			= load_qn>order_qn;
		var err			= lang['orders']['error_quantity'];
	}
	if(re){ 
		var result	= jQuery.parseJSON('{"info":[{"name":"'+$(parent).find('#quantity').attr('name')+'","value":"'+err+'"}]}');
		validity(result.info,$dom);
	}
}